package com.kotlin.sample.controller

import com.kotlin.sample.entity.MessageInfoEntity
import com.kotlin.sample.service.MessageInfoService
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/info")
class MessageInfoController(val service: MessageInfoService) {

    /**
     * http://localhost:8080/info?name=1
     */
    @GetMapping("/")
    fun index(@RequestParam("name") name: String) = "Hello, $name!"

    /**
     * http://localhost:8080/info
     */
    @GetMapping("/{id}")
    fun getById(@PathVariable id: String): List<MessageInfoEntity> =
        service.findMessageById(id)

    /**
     * http://localhost:8080/info/list-all
     */
    @GetMapping("/list-all")
    fun listAll(): List<MessageInfoEntity> = service.findMessages()

    /**
     * http://localhost:8080/info/save
     */
    @PostMapping("/save")
    fun save(@RequestBody message: MessageInfoEntity) {
        service.save(message)
    }

}